<?php

/**
 * Description of WithdrawController
 *
 * @author andro
 */
class WithdrawController extends AdminController {

    public function actionBonus() {

//        $withdraw = sys_ewallet_withdrawal::model()->findAll(array('condition' => 'ewallet_withdrawal_is_approve=:ewallet_withdrawal_is_approve',
//                                                                   'params' => array('ewallet_withdrawal_is_approve' => '0')));
        $data_withdraw = new CActiveDataProvider('sys_ewallet_withdrawal',
                        array('criteria' => array('condition' => 'ewallet_withdrawal_is_approve=:ewallet_withdrawal_is_approve',
                                'params' => array('ewallet_withdrawal_is_approve' => '0'))));
        $withdraw = $data_withdraw->getData();
        $this->render('withdraw', array('model' => $withdraw,
            'data_withdraw' => $data_withdraw));
    }

    public function actionSubmit() {
        if (isset($_POST['update'])) {
            if (is_array($_POST['withdraw'])) {
                Yii::import('application.extensions.excel_maker.excel');
                $xls = new excel('withdraw-' . function_lib::convert_datetime(date("Y-m-d H:i:s")) . ".xls");
                $total_withdraw = 0;
                $no = 1;
                $arr_withdraw = array('DAFTAR E-WALLET WITHDRAWAL ' . function_lib::convert_datetime(date("Y-m-d H:i:s"), " "));
                $xls->writeLine($arr_withdraw);
                $arr_withdraw = array('No', 'Member MID', 'Nama', 'Nama Bank', 'Nomor Rekening', 'No Handphone', 'Tanggal Withdrawal', 'Nominal Withdrawal');
                $xls->writeLine($arr_withdraw);
                foreach ($_POST['withdraw'] as $row => $value) {
                    //ambil satu2 datanya
                    $arr_network = Yii::app()->db->createCommand()->select('*')
                                    ->from('sys_ewallet_withdrawal')
                                    ->where('ewallet_withdrawal_id=:withdrawal_id', array('withdrawal_id' => $value))
                                    ->limit(1)->queryRow();
                    $arr_withdraw = array($no,
                        function_lib::get_network_mid($arr_network['ewallet_withdrawal_network_id']),
                        function_lib::get_member_name($arr_network['ewallet_withdrawal_network_id']),
                        function_lib::get_bank_name(function_lib::get_one('member_bank_id', 'sys_member', 'member_network_id = \'' . $arr_network['ewallet_withdrawal_network_id'] . '\'')),
                        function_lib::get_one('member_bank_account_no', 'sys_member', 'member_network_id = \'' . $arr_network['ewallet_withdrawal_network_id'] . '\''),
                        function_lib::get_one('member_mobilephone', 'sys_member', 'member_network_id = \'' . $arr_network['ewallet_withdrawal_network_id'] . '\''),
                        function_lib::convert_datetime($arr_network['ewallet_withdrawal_datetime'], ' '),
                        number_format($arr_network['ewallet_withdrawal_value']));
                    $xls->writeLine($arr_withdraw);
                    $no++;
                    $total_withdraw = $total_withdraw + $arr_network['ewallet_withdrawal_value'];
                }
                $arr_withdraw = array('', '', '', '', '', '', 'TOTAL', number_format($total_withdraw));
                $xls->writeLine($arr_withdraw);
                $xls->close();
            }
        }
    }

    public function actionExcel() {
        if (isset($_POST['update'])) {
            if (is_array($_POST['withdraw'])) {
                $file_name = "withdraw_" . date("Y-m-d-H-i-s");
                Yii::import('application.extensions.phpexcel.JPhpExcel');
                $xls = new JPhpExcel('UTF-8', false, 'Data Serial');
                $total_withdraw = 0;
                $no = 1;
                $arr_withdraw[1] = array('DAFTAR E-WALLET WITHDRAWAL ' . function_lib::convert_datetime(date("Y-m-d H:i:s"), " "));
                array_push($arr_withdraw, array('No', 'Member MID', 'Nama Rekening', 'Nama Bank', 'Nomor Rekening', 'Bank Cabang','No Handphone', 'Tanggal Withdrawal', 'Nominal Withdrawal'));
                $transaction = Yii::app()->db->beginTransaction();
                try {
                    foreach ($_POST['withdraw'] as $row => $value) {
                        //ambil satu2 datanya
                        $arr_network = Yii::app()->db->createCommand()->select('*')
                                        ->from('sys_ewallet_withdrawal')
                                        ->where('ewallet_withdrawal_id=:withdrawal_id', array('withdrawal_id' => $value))
                                        ->limit(1)->queryRow();
                        //update datanya
                        Yii::app()->db->createCommand()->update('sys_ewallet_withdrawal', array('ewallet_withdrawal_is_approve' => '1'),
                                                                'ewallet_withdrawal_id=:id',array(':id' => $value));
                        $note = "Withdraw E-wallet cash sebesar Rp.".  number_format($arr_network['ewallet_withdrawal_value']);
                        //nambah ke ewallet log
                        /*
                        Yii::app()->db->createCommand('INSERT INTO sys_ewallet_log SET ewallet_log_network_id = \''.$arr_network['ewallet_withdrawal_network_id'].'\',
                                                                                       ewallet_log_type = \'cash\',
                                                                                       ewallet_log_value = '.$arr_network['ewallet_withdrawal_value'].',
                                                                                       ewallet_log_status = \'debit\',
                                                                                       ewallet_log_datetime = \''.date("Y-m-d H:i:s").'\',
                                                                                       ewallet_log_note = \''.$note.'\'')->execute();
                        */
                        //update ke ewalletnya
//                        Yii::app()->db->createCommand("UPDATE sys_ewallet SET ewallet_cash_paid = ewallet_cash_paid + ".$arr_network['ewallet_withdrawal_value']."
//                                                       WHERE ewallet_network_id = '".$arr_network['ewallet_withdrawal_network_id']."'")->execute();
                        array_push($arr_withdraw, array($no,
                            function_lib::get_network_mid($arr_network['ewallet_withdrawal_network_id']),
                            //function_lib::get_member_name($arr_network['ewallet_withdrawal_network_id']),
                            function_lib::get_one('member_bank_account_name','sys_member','member_network_id = \''.$arr_network['ewallet_withdrawal_network_id'].'\''),
                            function_lib::get_bank_name(function_lib::get_one('member_bank_id', 'sys_member', 'member_network_id = \'' . $arr_network['ewallet_withdrawal_network_id'] . '\'')),
                            function_lib::get_one('member_bank_account_no', 'sys_member', 'member_network_id = \'' . $arr_network['ewallet_withdrawal_network_id'] . '\''),
                            function_lib::get_one('member_bank_branch','sys_member','member_network_id = \''.$arr_network['ewallet_withdrawal_network_id'].'\''),
                            function_lib::get_one('member_mobilephone', 'sys_member', 'member_network_id = \'' . $arr_network['ewallet_withdrawal_network_id'] . '\''),
                            function_lib::convert_datetime($arr_network['ewallet_withdrawal_datetime'], ' '),
                            intval($arr_network['ewallet_withdrawal_value'])));
                        $no++;
                        $total_withdraw = $total_withdraw + $arr_network['ewallet_withdrawal_value'];
                    }
                    $transaction->commit();
                    array_push($arr_withdraw, array('', '', '', '', '', '', 'TOTAL', intval($total_withdraw)));
                    //die(dirname($_SERVER['SCRIPT_FILENAME'])."/excel/withdraw/");
                    $xls->addArray($arr_withdraw);
                    //$xls->generateXML("withdraw_".date("Y-m-d-H-i-s"));
                    $xls->generate_excel(dirname($_SERVER['SCRIPT_FILENAME']) . "/excel/withdraw/" . $file_name);
                    Yii::app()->user->setFlash('success', 'withdraw approval success. Please <a href="' . Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls">download file excel</a>.');
                } catch (CDbException $e) {
                    $transaction->rollback();
                    Yii::app()->user->setFlash('error', 'Approval data withdraw have failed');
                } 
                $this->redirect(array('bonus'));
            }
        }
    }

}

?>
